"""
Case Type   : 内核告警
Case Name   : DN失效切换告警, 测试告警日志信息
Create At   : 2025-10-10
Owner       : lvlintao
Description :
    1、DN失效切换告警
    2、测试告警日志信息
    3、清理环境
Expect      :
    1、DN失效切换告警
    2、告警日志完整清晰
    3、清理环境成功
History     :
"""

import os
import unittest
import time

from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class AlarmReportTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.comm = Common('PrimaryDbUser')
        self.node = Node('PrimaryDbUser')
        self.std1_node = Node('Standby1DbUser')
        self.std2_node = Node('Standby2DbUser')
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_alarm_report(self):
        text = '--step1:DN失效切换告警;expect:成功--'
        logger.info(text)
        cmd = f"sed -i 's/port = {self.node.db_port}/port = 70000/g' {macro.DB_INSTANCE_PATH}/postgresql.conf;" \
              f"grep 'port =' {macro.DB_INSTANCE_PATH}/postgresql.conf"
        logger.info(cmd)
        res = self.node.sh(cmd).result()
        logger.info(res)
        pid = self.comm.get_pid(self.node, f"{macro.DB_INSTANCE_PATH}")
        logger.info(pid)
        cmd = f"kill -9 {pid}"
        res = self.node.sh(cmd).result()
        logger.info(res)
        self.assertNotIn('No such process', res, '执行失败' + text)
        time.sleep(30)
        status = self.comsh.get_db_cluster_status(param='detail')
        logger.info(status)

        text = '--step2:查看告警日志;expect:成功--'
        search_filename = f"cd {macro.PG_LOG_PATH}/../cm/cm_agent;ls -l | grep 'system_alarm' | awk '{{{{print $NF}}}}'"
        logger.info(search_filename)
        file1 = self.std1_node.sh(search_filename).result()
        logger.info(file1)
        cmd = f"cd {macro.PG_LOG_PATH}/../cm/cm_agent;tail -n 10 {file1}"
        res1 = self.std1_node.sh(cmd).result()
        logger.info(res1)
        file2 = self.std2_node.sh(search_filename).result()
        logger.info(file2)
        cmd = f"cd {macro.PG_LOG_PATH}/../cm/cm_agent;tail -n 10 {file2}"
        res2 = self.std2_node.sh(cmd).result()
        logger.info(res2)
        flag = 'DatanodeFailOver'
        self.assertTrue(flag in res1 or flag in res2, '执行失败' + text)

    def tearDown(self):
        text = '--step3:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        cmd = f"sed -i 's/port = 70000/port = {self.node.db_port}/g' {macro.DB_INSTANCE_PATH}/postgresql.conf"
        logger.info(cmd)
        res = self.node.sh(cmd).result()
        logger.info(res)
        time.sleep(5)
        cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
        res = self.node.sh(cmd).result()
        logger.info(res)
        if 'Primary' not in res:
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl switchover;"
            res = self.node.sh(cmd).result()
            logger.info(res)
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
            res = self.node.sh(cmd).result()
            logger.info(res)
        self.assertIn('Primary', res, '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
